﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace Htinns.GatewayConsole.Models
{
	public class GatewayContext:DbContext
	{
		public GatewayContext()
		    : base("GatewayConsole")
		{
		}
		public IDbSet<Vote> Votes { get; set; }

		public IDbSet<VoteItem> VoteItems { get; set; }

		public IDbSet<VoteSheet> VoteSheets { get; set; }

		public IDbSet<Article> Atricles { get; set; }

		public IDbSet<ModuleAssign> ModuleAssigns { get; set; }

		protected override void OnModelCreating(DbModelBuilder modelBuilder)
		{
			base.OnModelCreating(modelBuilder);
			
			//extend
			modelBuilder.Entity<VoteSheet>()
				.HasMany(s=>s.VoteItems)
				.WithMany(i=>i.VoteSheets)
				.Map(m => {
					m.ToTable("VoteSheetItem");
					m.MapLeftKey("VoteSheetId");
					m.MapRightKey("VoteItemId");
				});

		}
		
	}

	public class GatewayContextInitializer : DropCreateDatabaseIfModelChanges<GatewayContext>
	{

		protected override void Seed(GatewayContext context)
		{			
			System.Web.Mvc.UrlHelper url = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext);	

			new List<Article>{
			    new Article{
			        Title = "客房网络服务使用情况调查问卷",
			        Content = string.Format("<a class=\"dred\" href=\"{0}\">请告诉我们您的意见和建议</a>。", url.Action("Questionnaire","Home")),
			        CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
			        Creater = "Admin1", 
			        Operator = "Admin1" 
			    }			
			}.ForEach(a=>context.Atricles.Add(a));	
	
			var voteItems=new List<VoteItem>{
				new VoteItem{
					Content="有线网络",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin2",
					Creater="Admin2",
				},
				new VoteItem{
					Content="无线网络",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin3",
					Creater="Admin3",					
				},
				new VoteItem{
					Content="浏览网页慢",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin4",
					Creater="Admin4",
				},
				new VoteItem{
					Content="收发邮件慢",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin5",
					Creater="Admin5",
				},
				new VoteItem{
					Content="在线视频慢",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin6",
					Creater="Admin6",
				},
				new VoteItem{
					Content="网络游戏慢",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin7",
					Creater="Admin7",
				},
				new VoteItem{
					Content="下载慢",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin8",
					Creater="Admin8",
				},
				new VoteItem{
					Content="网络时断时续",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin9",
					Creater="Admin9",
				},
				new VoteItem{
					Content="无法自动获取IP地址",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin10",
					Creater="Admin10",
				},
				new VoteItem{
					Content="满意",					
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Operator="Admin",
					Creater="Admin",
				}
			};

			new List<Vote>{
				new Vote{
					Title="请问您对酒店客房的网络是否满意？",
					MaxSelect=10,
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Creater="Admin",
					Operator="Admin",	
					VoteItems=voteItems
				}
			}.ForEach(v => context.Votes.Add(v));

			new List<ModuleAssign>{
				new ModuleAssign{
					HotelId="0000000",
					ModuleId=1,
					ModuleType=0,
					Position=0,
					Valid=true,
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Creater="Admin",
					Operator="Admin"
				},
				new ModuleAssign{
					HotelId="0000000",
					ModuleId=1,
					ModuleType=1,
					Position=3,
					Valid=true,
					CreatedDate = DateTime.Now, 
			        ModifiedDate = DateTime.Now, 
					Creater="Admin",
					Operator="Admin"
				}
			}.ForEach(m => context.ModuleAssigns.Add(m));	
			
		}
	}
}